package com.pharmacy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pharmacy.dto.*;
import com.pharmacy.entity.Medicines;
import com.pharmacy.mapper.MedicinesMapper;
import com.pharmacy.result.PageResult;
import com.pharmacy.service.MedicinesService;
import com.pharmacy.vo.MedicinesVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class MedicinesServiceImpl implements MedicinesService {

    private static final Logger log = LoggerFactory.getLogger(MedicinesServiceImpl.class);
    @Autowired
    private MedicinesMapper medicinesMapper;


    /**
     * 添加药品
     * @param medicinesAddDto
     */
    @Override
    public void addMedinines(MedicinesAddDto medicinesAddDto) {
        Medicines medicines = new Medicines();
        BeanUtils.copyProperties(medicinesAddDto,medicines);
        medicines.setCreatedAt(LocalDateTime.now());
        medicines.setUpdatedAt(LocalDateTime.now());
        medicinesMapper.addMedicines(medicines);
    }

    /**
     * 根据条形码查询药品
     * @param barcode
     * @return
     */
    @Override
    public MedicinesVo getMedicines(String barcode) {

        MedicinesVo medicines = medicinesMapper.getMedicines(barcode);

        return medicines;
    }

    /**
     * 药品分页查询
     * @param medicinesPageSelectDto
     * @return
     */
    @Override
    public PageResult medicinesPageSelect(MedicinesPageSelectDto medicinesPageSelectDto) {

        PageHelper.startPage(medicinesPageSelectDto.getPage(),medicinesPageSelectDto.getPageSize());

        Page<MedicinesVo> page =  medicinesMapper.pageSelectMedicines(medicinesPageSelectDto);
        long total = page.getTotal();
        List<MedicinesVo> records = page.getResult();
        return new PageResult(total,records);
    }

    /**
     * 删除药品
     * @param id
     */
    @Override
    public void deleteMedicines(Integer id) {
        medicinesMapper.deleteMedicines(id);
    }

    /**
     * 修改药品
     * @param medicinesUpdateDto
     */
    @Override
    public void updateMedicines(MedicinesUpdateDto medicinesUpdateDto) {
        Medicines medicines = new Medicines();
        BeanUtils.copyProperties(medicinesUpdateDto,medicines);
        medicines.setUpdatedAt(LocalDateTime.now());
        medicinesMapper.updateMedicines(medicines);
    }

    /**
     * 根据id查询药品
     * @param id
     * @return
     */
    @Override
    public Medicines getById(Integer id) {
        Medicines medicines = medicinesMapper.getById(id);
        return medicines;
    }

    /**
     * 减少药品数量
     * @param reduceMedicineDto
     */
    @Override
    @Transactional
    public void reduceMedicine(ReduceMedicineDto reduceMedicineDto) {
        // 获取 products 列表
        List<Quantity> products = reduceMedicineDto.getProducts();

        log.info("减少药品数量:{}",products);
        // 遍历 products 列表
        for (Quantity product : products) {
            Integer id = product.getId();  // 获取药品ID
            Integer quantity = product.getQuantity();  // 获取减少的数量

            // 调用 batchReduceMedicine 方法更新药品数量
            medicinesMapper.batchReduceMedicine(id, quantity);
        }
    }



}
